<template>
  <el-card class="search">
    <!-- 头部 -->
    <template #header>
      <!-- 菜单选择器 -->
      <el-menu
        :default-active="activeIndex"
        class="el-menu-demo"
        mode="horizontal"
        @select="handleSelect"
      >
        <el-menu-item index="1">销售额</el-menu-item>
        <el-menu-item index="2">访问量</el-menu-item>
      </el-menu>

      <div class="right">
        <!-- 单选框选择器 -->
        <el-radio-group v-model="radio" style="margin-right: 12px">
          <el-radio-button label="今日" />
          <el-radio-button label="本周" />
          <el-radio-button label="本月" />
          <el-radio-button label="今年" />
        </el-radio-group>

        <!-- 日期选择器 -->
        <el-date-picker
          v-model="dateValue"
          type="daterange"
          unlink-panels
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :shortcuts="shortcuts"
        />
      </div>
    </template>
    <!-- 内容部分 -->
    <div class="content">
      <v-chart :option="barOption()" autoresize></v-chart>
      <div class="content-right">
        <div class="sale-title">门店销售额排名</div>
        <div class="list">
          <div class="list-item" v-for="item in rankData">
            <div class="no" :class="item.no < 4 ? 'top-no' : ''">
              {{ item.no }}
            </div>
            <div class="name">{{ item.name }}</div>
            <div class="count">{{ item.count }}</div>
          </div>
        </div>
      </div>
    </div>
  </el-card>
</template>

<script lang="ts">
export default {
  name: "Search",
}
</script>

<script lang="ts" setup>
import { ref, onMounted, computed, watch } from "vue"
import { useReportDataStore } from "@/stores/reportData"
import { formatData } from "@/hooks/useFormat"
const reportDataStore = useReportDataStore()
const { numberFormat, moneyFormat } = formatData()

// 销售额与访问量
const orderFullYearAxis = computed(
  () => reportDataStore.reportData.orderFullYearAxis
)
const orderFUllYear = computed(() => reportDataStore.reportData.orderFullYear)
const userFullYearAxis = computed(
  () => reportDataStore.reportData.userFullYearAxis
)
const userFullYear = computed(() => reportDataStore.reportData.userFullYear)

// 排行榜
const orderRank = computed(() => reportDataStore.reportData.orderRank)
const userRank = computed(() => reportDataStore.reportData.userRank)
const rankData = computed(() =>
  activeIndex.value === "1" ? orderRank.value : userRank.value
)

// 菜单相关变量函数
const activeIndex = ref("1")
const handleSelect = (key: string) => {
  activeIndex.value = key
}

// 单选框
const radio = ref("本周")

// 日期选择器相关变量
const dateValue = ref()
const shortcuts = [
  {
    text: "最近一周",
    value: () => {
      const end = new Date()
      const start = new Date()
      start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
      return [start, end]
    },
  },
  {
    text: "最近一个月",
    value: () => {
      const end = new Date()
      const start = new Date()
      start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
      return [start, end]
    },
  },
  {
    text: "最近三个月",
    value: () => {
      const end = new Date()
      const start = new Date()
      start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
      return [start, end]
    },
  },
]

// echarts柱状图
const barOption = () => {
  let text = activeIndex.value === "1" ? "销售趋势" : "访问量趋势"
  return {
    title: {
      text,
      textStyle: {
        fontSize: 14,
      },
    },
    tooltip: {
      trigger: "axis",
      axisPointer: {
        type: "shadow",
      },
    },
    xAxis: {
      type: "category",
      data:
        activeIndex.value === "1"
          ? orderFullYearAxis.value
          : userFullYearAxis.value,
    },
    yAxis: {
      splitLine: {
        lineStyle: {
          type: "dotted",
          color: "#eee",
        },
      },
    },
    grid: {
      left: 50,
      right: 50,
      bottom: 50,
      top: 50,
    },
    series: [
      {
        data:
          activeIndex.value === "1" ? orderFUllYear.value : userFullYear.value,
        type: "bar",
        barWidth: "35%",
      },
    ],
  }
}
</script>

<style lang="scss" scope>
.search {
  margin: 20px 0;
  position: relative;
  .el-card__header {
    border-bottom: none;
    padding: 0 20px;
  }
  .right {
    position: absolute;
    top: 16px;
    right: 20px;
    display: flex;
  }
  .content {
    display: flex;
    height: 270px;
    .echarts {
      flex: 5;
    }
    .content-right {
      flex: 2;

      .list {
        margin-top: 15px;

        .list-item {
          display: flex;
          align-items: center;
          height: 20px;
          font-size: 12px;
          margin: 6px 20px 6px 0;

          .no {
            width: 20px;
            height: 20px;
            color: #333;
            display: flex;
            justify-content: center;
            align-items: center;

            &.top-no {
              border-radius: 50%;
              background-color: #000;
              color: #fff;
              font-weight: 300;
            }
          }

          .name {
            margin-left: 10px;
          }

          .count {
            flex: 1;
            text-align: right;
          }
        }
      }
    }
  }
}
</style>
